package com.wbao.dianniu.im;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.alibaba.mobileim.YWIMKit;
import com.alibaba.mobileim.conversation.IYWConversationService;
import com.alibaba.mobileim.conversation.YWConversation;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase.Mode;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase.OnRefreshListener;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshListView;
import com.wbao.dianniu.R;
import com.wbao.dianniu.data.NoticeQuestData;
import com.wbao.dianniu.listener.INoticeQuestListListener;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.NoticeQuestListManager;
import com.wbao.dianniu.ui.BaseActivity;
import com.wbao.dianniu.ui.QaDetailActivity;
import com.wbao.dianniu.utils.PictureDownloadUtils;

public class QaHelperActivity extends BaseActivity implements INoticeQuestListListener {
	private PullToRefreshListView pullListView;
	private ListView mListView;
	private NoticeQuestListManager manager;
	private MyAdapter adapter;
	private List<NoticeQuestData> mList;
	private final int PAGE_COUNTS = 10;
	private int currentPage = 0;
	private final int type = 1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentLayout(R.layout.activity_notice_qa_helper);
		setTitleName("问答通知");
		init();
		requestData(currentPage);
		YWIMKit iMkit = LoginSampleHelper.getInstance().getIMKit();
		IYWConversationService conversationService = iMkit.getConversationService();
		YWConversation conversation = conversationService.getConversationByUserId("119", LoginSampleHelper.APP_KEY);
		if(null != conversation)
			conversationService.markReaded(conversation);
	}
	
	private void init() {
		pullListView = (PullToRefreshListView) findViewById(R.id.notice_qa_listview);
		pullListView.setMode(Mode.PULL_UP_TO_REFRESH);
		pullListView.setOnRefreshListener(onRefreshListener);
		mListView = pullListView.getRefreshableView();
		registerForContextMenu(mListView);
		
		adapter = new MyAdapter(this);
		mList = new ArrayList<>();
		adapter.addData(mList);
		mListView.setAdapter(adapter);
		mListView.setOnItemClickListener(itemClickListener);
		
	}
	OnRefreshListener onRefreshListener = new OnRefreshListener() {
		
		@Override
		public void onRefresh() {
			//  上拉刷新 加载请求数据
			pullUpRequest();
		}
	};
	
	OnItemClickListener itemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO Auto-generated method stub
			
		}
	};
	
	// 上拉刷新数据请求
		private void pullUpRequest(){
			//没有搜索结果 或者数据为空 上拉没有效果
			++currentPage ;
			requestData(currentPage);
		}
		private void requestData(int page) {
			manager = new NoticeQuestListManager(this);
			manager.addNoticeQuestListListener(this);
			manager.noticeQuestList(GlobalContext.accountId, type, page * PAGE_COUNTS, PAGE_COUNTS);
		}

		@Override
		public void onNoticeQuestListFailure(int errorCode, String errorMsg) {
			pullListView.onRefreshComplete(false, false);
		}

		@Override
		public void onNoticeQuestListSuccess(List<NoticeQuestData> list) {
			// TODO Auto-generated method stub
			pullListView.onRefreshComplete(false, false);
			if(null == list)
				return;
			adapter.addData(list);
			mList = adapter.getList();
			adapter.notifyDataSetChanged();
		}
		
		class MyAdapter extends BaseAdapter{

			private Context mContext;
			private List<NoticeQuestData> list;
			private LayoutInflater mInflater;
			public MyAdapter(Context context) {
				mContext = context;
				mInflater = LayoutInflater.from(mContext);
				if(null == list)
					list = new ArrayList<NoticeQuestData>();
			}
			
			public void addData(List<NoticeQuestData> addList){
				for (int i = 0; i < addList.size(); i++) {
					list.add(addList.get(i));
				}
			}
			public List<NoticeQuestData> getList() {
				return list;
			}
			@Override
			public int getCount() {
				if (null == list)
					return 0;
				else
					return list.size();
			}

			@Override
			public Object getItem(int position) {
				return list.get(position);
			}

			@Override
			public long getItemId(int position) {
				return position;
			}

			@Override
			public View getView(final int position, View convertView, ViewGroup parent) {
				Holder holder = null;
				if (convertView == null) {
					convertView = mInflater.inflate(R.layout.notice_qa_item, null);   
					holder = new Holder();
					holder.head = (ImageView)convertView.findViewById(R.id.user_head);
					holder.name =  (TextView)convertView.findViewById(R.id.user_name);
					holder.label =  (TextView)convertView.findViewById(R.id.user_label);
					holder.date =  (TextView)convertView.findViewById(R.id.user_date);
					holder.answerContent =  (TextView)convertView.findViewById(R.id.answer_content);
					holder.qaContent =  (TextView)convertView.findViewById(R.id.answer_qa_content);
					convertView.setTag(holder); 
				}else{
					 holder = (Holder) convertView.getTag();
				}
				holder.date.setVisibility(View.VISIBLE);
				NoticeQuestData data = list.get(position);
				PictureDownloadUtils.displayHeadImage(mContext,holder.head, data.getSendHeadPic());
				holder.name.setText(data.getSendRealName() == null ? "": data.getSendRealName());
				holder.label.setText(data.getSendLabel() == null ? "" : data.getSendLabel());
				holder.date.setText(data.getCreateDate() == null ? "" : data.getCreateDate());
				holder.answerContent.setText(data.getContent() == null ? "" : data.getContent());
				holder.qaContent.setText(data.getQuestContent() == null ? "" : "问答：" + data.getQuestContent());
				holder.answerContent.setOnClickListener(new MyClickListener(data));
				holder.qaContent.setOnClickListener(new MyClickListener(data));
				return convertView;
			}
			
			class Holder{
				public ImageView head;
				public TextView name;
				public TextView label;
				public TextView date;
				public TextView answerContent;
				public TextView qaContent;
			}
			
			class MyClickListener implements OnClickListener{

				private NoticeQuestData data;
				public MyClickListener(NoticeQuestData data) {
					this.data = data;
				}
				
				@Override
				public void onClick(View v) {
					switch (v.getId()) {
					case R.id.answer_content:
					case R.id.answer_qa_content:
						Intent contentIntent = new Intent();
						Bundle bundle = new Bundle();
						bundle.putString(GlobalContext.BUNDLE_HEADPIC, data.getQuestHeadPic());
						bundle.putString(GlobalContext.BUNDLE_REALNAME, data.getQuestRealName());
						bundle.putString(GlobalContext.BUNDLE_LABEL, data.getQuestLabel());
						bundle.putInt(GlobalContext.BUNDLE_ISGOOD, data.isQuestIsGood());
						bundle.putString(GlobalContext.BUNDLE_CONTENT, data.getQuestContent());
						bundle.putInt(GlobalContext.BUNDLE_VISITORCOUNT, data.getQuenstVisitorCount());
						bundle.putInt(GlobalContext.BUNDLE_FORWARDCOUNT, data.getQuestForwardCount());
						bundle.putInt(GlobalContext.BUNDLE_GOODCOUNT, data.getQuestGoodCount());
						bundle.putInt(GlobalContext.BUNDLE_ANSWERCOUNT, data.getQuestAnswerCount());
						bundle.putInt(GlobalContext.BUNDLE_ACCOUNTID, 	data.getQuestAccountId());
						bundle.putInt(GlobalContext.BUNDLE_QUESTID, data.getQuestId());
						bundle.putString(GlobalContext.BUNDLE_QUESTIMGS, data.getQuestImgs());
						contentIntent.putExtra(GlobalContext.QA_DATA_BUNDLE, bundle);
						contentIntent.setClass(mContext, QaDetailActivity.class);
						startActivity(contentIntent);
						
						break;

					default:
						break;
					}
				}
				
			}
			
		}
	
}
